#pragma once
#ifndef XMLPROCCESSOR_H
#define XMLPROCCESSOR_H

#include "ResultMessage.h"
#include <QString>
#include <QtXml>
#include "Command.h"
#include "Result.h"
#define RUN             "Run"
#define KILL            "Kill"
#define PING            "Ping"
#define RESET           "Reset"
#define TRANSMITGRAMMAR "TransmitGrammar"
#define ROLLBACK        "RollBackGrammar"
namespace XmlProccessor {



inline ResultMessage ExtractContent(QString xml)
{
    ResultMessage message;
    QXmlStreamReader reader(xml);
    while(!reader.isStartElement())
    {
        reader.readNext();
    }
    message.Target= reader.attributes().value("target").toString();
    message.Content=xml;
    return message;
}

inline QString generateXmlRemoteMessage(int mode)
{
    char CommandText[][17]={RUN,KILL,PING,RESET,TRANSMITGRAMMAR,ROLLBACK};
    QString xml_str;
    QXmlStreamWriter writer(&xml_str);
    writer.writeStartElement("Message");
        writer.writeAttribute("target","RemoteController");
        writer.writeTextElement("Command",CommandText[mode]);
    writer.writeEndElement();

    return xml_str+"||";
}

inline QString extractRemoteResultMessage(QString xml)
{
    QXmlStreamReader reader(xml);
    while(!reader.isStartElement())
    {
        reader.readNext();
    }
    return reader.readElementText(QXmlStreamReader::IncludeChildElements);
}

inline QString extractLogMessage(QString xml)
{
    QXmlStreamReader reader(xml);
    while(!reader.isStartElement())
    {
        reader.readNext();
    }
    return reader.readElementText(QXmlStreamReader::IncludeChildElements);
}


inline QString Generate(Command c)
{
    QString xml_str;
    QXmlStreamWriter writer(&xml_str);
    writer.writeStartElement("Message");
    writer.writeAttribute("target","SpeechServer");
        writer.writeStartElement("Command");
        writer.writeTextElement("CommandType",QString::fromStdString(c.commandtype));
            writer.writeStartElement("Args");
                for(unsigned int i=0;i<c.args.size();i++)
                {
                    writer.writeStartElement("arg");
                            writer.writeTextElement("name",QString::fromStdString(c.args[i].name));
                            writer.writeTextElement("data",QString::fromStdString(c.args[i].data));
                    writer.writeEndElement();
                }
            writer.writeEndElement();
        writer.writeEndElement();
    writer.writeEndElement();
    return xml_str+"||";
}



inline Result Parse(QString str)
{
    str=str.left(str.indexOf("||"));
    //ROS_INFO(str.c_str());
    if(str=="")
        return Result(false);
    Result result;
    QXmlStreamReader reader(str);

    while(!reader.atEnd() && !reader.hasError())
    {
        QXmlStreamReader::TokenType token=reader.readNext();
        if(token == QXmlStreamReader::StartElement)
        {
            QStringRef name= reader.name();
            if(name == "Result")
            {
               continue;
            }
            if(name == "Succeed")
            {
                result.success= (reader.readElementText().toLower()=="true"?true:false);
            }
            if(name == "Command")
            {
                result.command=reader.readElementText().toStdString();
            }
            if(name == "Text")
            {
                result.text=reader.readElementText().toStdString();
            }
//                if(name == "Hypothesizes")
//                {
//                    token=reader.readNext();
//                    name= reader.name();
//                    while(name=="Hypo")
//                    {
//                        result.hypothesizes.push_back(reader.readElementText().toStdString());
//                        token=reader.readNext();
//                        name= reader.name();
//                    }
//                }
            if(name == "Confidence")
            {
                result.confidence=reader.readElementText().toFloat();
            }
            if(name == "Position")
            {
                result.position=reader.readElementText().toFloat();
            }
            if(name == "Position_Confidence")
            {
                result.position_confidence=reader.readElementText().toFloat();
            }
        }
    }
    return result;
}
}
#endif // XMLPROCCESSOR_H
